package com.ruoyi.system.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.CarCoupon;
import com.ruoyi.system.service.ICarCouponService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 商家优惠券Controller
 * 
 * @author ruoyi
 * @date 2025-08-13
 */
@RestController
@RequestMapping("/coupon")
public class CarCouponController extends BaseController
{
    @Autowired
    private ICarCouponService carCouponService;

    /**
     * 查询商家优惠券列表
     */
    @RequiresPermissions("system:coupon:list")
    @GetMapping("/list")
    public TableDataInfo list(CarCoupon carCoupon)
    {
        startPage();
        List<CarCoupon> list = carCouponService.selectCarCouponList(carCoupon);
        return getDataTable(list);
    }

    /**
     * 导出商家优惠券列表
     */
    @RequiresPermissions("system:coupon:export")
    @Log(title = "商家优惠券", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CarCoupon carCoupon)
    {
        List<CarCoupon> list = carCouponService.selectCarCouponList(carCoupon);
        ExcelUtil<CarCoupon> util = new ExcelUtil<CarCoupon>(CarCoupon.class);
        util.exportExcel(response, list, "商家优惠券数据");
    }

    /**
     * 获取商家优惠券详细信息
     */
    @RequiresPermissions("system:coupon:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(carCouponService.selectCarCouponById(id));
    }

    /**
     * 新增商家优惠券
     */
//    @RequiresPermissions("system:coupon:add")
    @Log(title = "商家优惠券", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CarCoupon carCoupon)
    {
        return toAjax(carCouponService.insertCarCoupon(carCoupon));
    }

    /**
     * 修改商家优惠券
     */
    @RequiresPermissions("system:coupon:edit")
    @Log(title = "商家优惠券", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CarCoupon carCoupon)
    {
        return toAjax(carCouponService.updateCarCoupon(carCoupon));
    }

    /**
     * 删除商家优惠券
     */
    @RequiresPermissions("system:coupon:remove")
    @Log(title = "商家优惠券", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(carCouponService.deleteCarCouponByIds(ids));
    }

    //根据商户id查找优惠券
    @GetMapping("/list/mid/{mid}")
    public List<CarCoupon> listCouponByMerchantId(@PathVariable Integer mid)
    {
    	return carCouponService.listCouponByMerchantId(mid);
    }
}
